package servlets;

import fachadas.FachadaDePerguntas;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Diego
 */
public class CadastroDeTemas extends HttpServlet {
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       String tema = request.getParameter("tema");
       FachadaDePerguntas p = FachadaDePerguntas.getInstance();
       List<String> lista = new ArrayList<String>();
       ServletContext contexto = getServletContext();
       String texto = "";
       boolean inseriu = true;
        
       try { 
            p.inserirTema(tema);
            
             lista = p.atualizarTema();
            
            if(!lista.isEmpty()){
                int i = 0;
                
                while(i < lista.size()){
                    texto = texto + lista.get(i);
                    
                    if(i < lista.size()){
                        texto = texto + ",";
                    }
                    i++;
                }
                contexto.setAttribute("Opcoes", texto);
            }
        } catch (SQLException ex) {
            inseriu = false;
        }
        request.setAttribute("inseriu", inseriu);
        
        request.getRequestDispatcher("resultadoDeInsercoes.jsp").forward(request,response);
    } 

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
